package com.gtmc.ecm.common.core.utils;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 共享文件操作工具类
 * @author: zhz
 * @create: 2021/12/28 8:58
 */
@Slf4j
public class SmbUtil {

    /**
     * 获取smbFile连接
     *
     * @param remoteUrl
     * @return
     */
    private static SmbFile getSmbClient(String remoteUrl) {
        long start = System.currentTimeMillis();
        SmbFile smbFile = null;
        try {
            log.info("开始连接...url：" + remoteUrl);
            smbFile = new SmbFile(remoteUrl);
            smbFile.connect();
            long end = System.currentTimeMillis();
            log.info("连接成功...url：" + remoteUrl + ", 连接耗时：" + (end - start) + " ms");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return smbFile;
    }

    /**
     * 获取共享文件夹下文件
     *
     * @param remotePath 共享文件服务路径
     * @return
     */
    public static List<String> smbGetAll(String remotePath) {
        List<String> list = new ArrayList<>();
        String dataStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        try {
            SmbFile file = new SmbFile(remotePath);
            if (file.exists()) {
                SmbFile[] smbFiles = file.listFiles();
                for (SmbFile f : smbFiles) {
                    if (f.toString().contains(dataStr)) {
                        smbGet(remotePath + f.getName());
                        list.add(f.getName());
                    }
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 从局域网中共享文件中得到文件并保存在本地磁盘上
     *
     * @param remoteUrl 共享文件服务路径
     */
    public static void smbGet(String remoteUrl) {

        InputStream in = null;
        OutputStream out = null;
        SmbFile smbFile = getSmbClient(remoteUrl);
        try {
            if (!smbFile.exists()) {
                throw new Exception("共享文件不存在");
            }
            String fileName = smbFile.getName();
            in = new BufferedInputStream(new SmbFileInputStream(smbFile));
            out = new BufferedOutputStream(new FileOutputStream(fileName));
            byte[] buffer = new byte[1024];
            while ((in.read(buffer)) != -1) {
                out.write(buffer);
                buffer = new byte[1024];
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 把本地磁盘中的文件上传到局域网共享文件下
     *
     * @param remoteUrl     共享电脑路径
     * @param localFilePath 本地路径 如：D:/
     */
    public static void smbPut(String remoteUrl, String localFilePath) {
        InputStream in = null;
        OutputStream out = null;
        File localFile = new File(localFilePath);
        String fileName = localFile.getName();
        SmbFile smbClient = getSmbClient(remoteUrl);
        try {
            if (!smbClient.isDirectory()) {
                smbClient.mkdirs();
            }
            SmbFile remoteFile = new SmbFile(remoteUrl + File.separator + fileName);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[1024];
            while ((in.read(buffer)) != -1) {
                out.write(buffer);
                buffer = new byte[1024];
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
